﻿package morn.core.utils
{
    import __AS3__.vec.*;
    import flash.display.*;
    import flash.geom.*;

    public class BitmapUtils extends Object
    {
        private static var m:Matrix = new Matrix();
        private static var newRect:Rectangle = new Rectangle();
        private static var clipRect:Rectangle = new Rectangle();
        private static var grid:Rectangle = new Rectangle();

        public function BitmapUtils()
        {
            return;
        }// end function

        public static function scale9Bmd(param1:BitmapData, param2:Array, param3:int, param4:int) : BitmapData
        {
            var _loc_9:Array = null;
            var _loc_10:Array = null;
            var _loc_11:Array = null;
            var _loc_12:Array = null;
            var _loc_13:int = 0;
            var _loc_14:int = 0;
            var _loc_15:BitmapData = null;
            var _loc_16:Shape = null;
            if (param1.width == param3)
            {
                if (param1.height == param4)
                {
                    return param1.clone();
                }
            }
            param3 = param3 > 1 ? (param3) : (1);
            param4 = param4 > 1 ? (param4) : (1);
            var _loc_5:* = int(param2[0]) + int(param2[2]);
            var _loc_6:* = int(param2[1]) + int(param2[3]);
            var _loc_7:* = new BitmapData(param3, param4, param1.transparent, 0);
            var _loc_8:Boolean = false;
            if (param2.length > 4)
            {
                _loc_8 = param2[4] == true;
            }
            if (param3 > _loc_5)
            {
                if (param4 > _loc_6)
                {
                    setRect(grid, param2[0], param2[1], param1.width - param2[0] - param2[2], param1.height - param2[1] - param2[3]);
                    _loc_9 = [0, grid.top, grid.bottom, param1.height];
                    _loc_10 = [0, grid.left, grid.right, param1.width];
                    _loc_11 = [0, grid.top, param4 - (param1.height - grid.bottom), param4];
                    _loc_12 = [0, grid.left, param3 - (param1.width - grid.right), param3];
                    _loc_13 = 0;
                    _loc_14 = 0;
                    if (_loc_8)
                    {
                        _loc_16 = new Shape();
                        _loc_13 = 0;
                        while (_loc_13 < 3)
                        {
                            
                            _loc_14 = 0;
                            while (_loc_14 < 3)
                            {
                                
                                setRect(newRect, _loc_10[_loc_13], _loc_9[_loc_14], _loc_10[(_loc_13 + 1)] - _loc_10[_loc_13], _loc_9[(_loc_14 + 1)] - _loc_9[_loc_14]);
                                setRect(clipRect, _loc_12[_loc_13], _loc_11[_loc_14], _loc_12[(_loc_13 + 1)] - _loc_12[_loc_13], _loc_11[(_loc_14 + 1)] - _loc_11[_loc_14]);
                                _loc_15 = new BitmapData(newRect.width, newRect.height, param1.transparent, 0);
                                m.identity();
                                m.tx = -newRect.x;
                                m.ty = -newRect.y;
                                newRect.x = 0;
                                newRect.y = 0;
                                _loc_15.draw(param1, m, null, null, newRect);
                                _loc_16.graphics.beginBitmapFill(_loc_15);
                                _loc_16.graphics.drawRect(0, 0, clipRect.width, clipRect.height);
                                _loc_16.graphics.endFill();
                                m.identity();
                                m.tx = clipRect.x - newRect.x * m.a;
                                m.ty = clipRect.y - newRect.y * m.d;
                                _loc_7.draw(_loc_16, m, null, null, clipRect, true);
                                _loc_16.graphics.clear();
                                _loc_15.dispose();
                                _loc_14++;
                            }
                            _loc_13++;
                        }
                        _loc_16 = null;
                        _loc_15 = null;
                    }
                    else
                    {
                        _loc_13 = 0;
                        while (_loc_13 < 3)
                        {
                            
                            _loc_14 = 0;
                            while (_loc_14 < 3)
                            {
                                
                                setRect(newRect, _loc_10[_loc_13], _loc_9[_loc_14], _loc_10[(_loc_13 + 1)] - _loc_10[_loc_13], _loc_9[(_loc_14 + 1)] - _loc_9[_loc_14]);
                                setRect(clipRect, _loc_12[_loc_13], _loc_11[_loc_14], _loc_12[(_loc_13 + 1)] - _loc_12[_loc_13], _loc_11[(_loc_14 + 1)] - _loc_11[_loc_14]);
                                m.identity();
                                m.a = clipRect.width / newRect.width;
                                m.d = clipRect.height / newRect.height;
                                m.tx = clipRect.x - newRect.x * m.a;
                                m.ty = clipRect.y - newRect.y * m.d;
                                _loc_7.draw(param1, m, null, null, clipRect, true);
                                _loc_14++;
                            }
                            _loc_13++;
                        }
                    }
                }
                else
                {
                    m.identity();
                    m.scale(param3 / param1.width, param4 / param1.height);
                    setRect(grid, 0, 0, param3, param4);
                    _loc_7.draw(param1, m, null, null, grid, true);
                }
            }
            else
            {
                m.identity();
                m.scale(param3 / param1.width, param4 / param1.height);
                setRect(grid, 0, 0, param3, param4);
                _loc_7.draw(param1, m, null, null, grid, true);
            }
            return _loc_7;
        }// end function

        public static function setRect(param1:Rectangle, param2:Number = 0, param3:Number = 0, param4:Number = 0, param5:Number = 0) : Rectangle
        {
            param1.x = param2;
            param1.y = param3;
            param1.width = param4;
            param1.height = param5;
            return param1;
        }// end function

        public static function createClips(param1:BitmapData, param2:int, param3:int) : Vector.<BitmapData>
        {
            var _loc_9:int = 0;
            var _loc_10:BitmapData = null;
            if (param1 == null)
            {
                return null;
            }
            var _loc_4:* = new Vector.<BitmapData>;
            var _loc_5:* = Math.max(param1.width / param2, 1);
            var _loc_6:* = Math.max(param1.height / param3, 1);
            var _loc_7:* = new Point();
            var _loc_8:int = 0;
            while (_loc_8 < param3)
            {
                
                _loc_9 = 0;
                while (_loc_9 < param2)
                {
                    
                    _loc_10 = new BitmapData(_loc_5, _loc_6);
                    _loc_10.copyPixels(param1, new Rectangle(_loc_9 * _loc_5, _loc_8 * _loc_6, _loc_5, _loc_6), _loc_7);
                    _loc_4.push(_loc_10);
                    _loc_9++;
                }
                _loc_8++;
            }
            return _loc_4;
        }// end function

    }
}
